//-----------------公用函数-----------------
// HTML转义
function htmlEncode (value){
    return !value ? value : String(value).replace(/&/g, "&amp;").replace(/>/g, "&gt;").replace(/</g, "&lt;").replace(/"/g, "&quot;").replace(/\s{1}/g,"&nbsp;");
}
// 改良的split内循环回调处理函数
function splitEach(strObj,spliter,callback){
    var arr = strObj.split(spliter)
    for(var i=0;i<arr.length;i++){
        callback(i,arr[i])
    }
}
// 改良的数组内循环回调处理函数
function arrEach(arrObj,callback){
    for(var i=0;i<arrObj.length;i++){
        callback(i,arrObj[i])
    }
}

function toTOC(){
    try{
        var doc = window.frames["if1"].document
        var txt2 =$(doc).find(".main").html();
        //替换生成的空p标签--<p></p>
        txt2 = txt2.replace(/<p><\/p>/g,"");
        //替换/TOC为目录内容
        var match= txt2.match(/\/TOC/g);
        txt2 =txt2.replace(match[0],TOCstr())
        $(doc).find(".main").html(txt2);
    }catch(err) {}
}
function BNPdiv(order,book){
    return "<div class='" + order +"'>" + eval("book." + order) + "</div>"
}

function setValOrder(txt,order,callback){
    try{
        var reg =eval("/\\/" + order + "[{]{1}.*[}]{1}/g");
        var match= txt.match(reg);
        var pm = match[0].replace("/" + order + "{","");
            pm = pm.replace("}","")
        callback(pm,match[0])
    }catch(err) {
        console.log(err);
    }
}
//宏变量引用替换处理函数
function $Orders(txt,callback,callback2){


    var reg1 =eval("/\\$[(]{1}.*[)]{1}/g");//匹配 $(var_name)
    var match1= txt.match(reg1);
    try {
        for(var i=0;i<match1.length;i++){//遍历每处正文引用
            var m_prms = prmsSmall(match1[i],"$")//指令的参数部分
            callback(i,match1[i],m_prms);
        }
    } catch (err) {
        console.log(err);
    }
}

//模板宏变量引用替换处理函数
function $$Orders(txt,callback,callback2){
    var reg1 =eval("/\\${2}[(]{1}.*[)]{1}/g");//匹配 $$(var_name)
    var reg2 =eval("/\\${2}[(]{1}.*[)]{1}/g");//匹配 $$[var_name]
    var match1= txt.match(reg1);
    var match2= txt.match(reg2);
    try {
        for(var i=0;i<match1.length;i++){//遍历每处正文引用
            var m_prms = prmsSmall(match1[i],"$$")//指令的参数部分
            callback(i,match1[i],m_prms);
        }
        for(var i=0;i<match2.length;i++){//遍历每处参数引用

        }
    } catch (err) {
        console.log(err);
    }
}


//区块指令处理函数
function areaOrders(txt,order,callback){
    try {
        if(order == "$" || order == "${2}"){
            order = "\\" + order
        }
        var reg = eval("/\\/" + order + "[{]{1}.*[}]{1}[\\s\\S]*?\\/{2}" + order + "/gm");
        var m1 = txt.match(reg);
        arrEach(m1,function(i,m){//遍历每一个指令
            var lines = m.split("\n")
            var head = lines[0]//指令头部
            var foot = lines[lines.length-1]//指令尾部
            //记录区
            var recsStr = m.replace(head +"\n","")
                recsStr = recsStr.replace(foot ,"")
            var m_prms = prms(head,order)//指令参数

            callback(i,m,head,foot,m_prms,recsStr);
        });
    } catch (err) {
        console.log(err);
    }
}

//返回一个起始标签的参数字符串
function prms(match,order){
    var od =eval("/\\/" + order + "[{]{1}/g")
    var prm = match.replace(od,"");
        prm = prm.replace("}","")
    return prm;
}
//返回一个小括号的参数字符串
function prmsSmall(match,order){
    var prm = match.replace(order + "(","");
        prm = prm.replace(")","")
    return prm;
}
//返回一个起始标签的参数字符串
function prmsMiddle(match,order){
    var prm = match.replace("/" + order + "{","");
        prm = prm.replace("}","")
    return prm;
}


//返回选择题选项的ABCD...字符
function numToABC(num){
    switch (num) {
        case 0:Tag = "A";break;
        case 1:Tag = "B";break;
        case 2:Tag = "C";break;
        case 3:Tag = "D";break;
        case 4:Tag = "E";break;
        case 5:Tag = "F";break;
        case 6:Tag = "G";break;
    }
    return Tag;
}
//数字转汉字--用于汉字编号的生成
function numTochinese(num){
    var numStr ="";
    var chineseNums = ["零","一","二","三","四","五","六","七","八","九"]
    if(num < 100000){//只处理小于十万的正整数
        var numStr = String(num).split('').reverse().join('');
        var numStr2 = "";
        for(var j=0;j<numStr.length;j++){
            var chNum = "";
            if(j>=3 && Number(numStr[j]) == 2){
                chNum = "两";
            }else{
                if(numStr.length = 3 && j == 2 && Number(numStr[j]) == 2){
                    chNum = "两";
                }else{
                    chNum = chineseNums[Number(numStr[j])];
                }
                
            }
            
            switch (j) {
                case 0:numStr2 += chNum;break;
                case 1:numStr2 += "十" + chNum;break;
                case 2:numStr2 += "百" + chNum;break;
                case 3:numStr2 += "千" + chNum;break;
                case 4:numStr2 += "万" + chNum;break;
            }
        }

    }
    return numStr2.split('').reverse().join('');
}

//返回生成的目录字符串
function TOCstr(){
    var TOC = "";
    //h1
    TOC += "<ul>"
    var pageBK="<div class='pageBreak'></div>"//分页
    var doc = window.frames["if1"].document
    $(doc).find(".main>h1").each(function(){
        var h1Itm = "<li>" + $(this).text() + "<span></span></li>";
            TOC += h1Itm;
            //h2
            TOC += "<ul>"
            $(this).nextUntil("h1").filter("h2").each(function(){
                var h2Itm = "<li>" + $(this).text() + "<span></span></li>";
                    TOC += h2Itm;
                    //h3
                    TOC += "<ul>"
                    $(this).nextUntil("h2").filter("h3").each(function(){
                        var h3Itm = "<li>" + $(this).text() + "<span></span></li>";
                            TOC += h3Itm;
                            //h4
                            TOC += "<ul>"
                            $(this).nextUntil("h3").filter("h4").each(function(){
                                var h4Itm = "<li>" + $(this).text() + "<span></span></li>";
                                    TOC += h4Itm;
                            });
                            TOC += "</ul>";
                    });
                    TOC += "</ul>";
            });
            TOC += "</ul>";
    });
    return "<div class='TOC'><h1>目 录</h1>" + TOC + "</ul></ul></ul></div>";
}